using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace ASPNETVN.PORTAL.Modules.LINK.Components
{
    public partial class Item
    {
        #region Private members.

        protected int _ID;
        protected int _ModuleID;
        protected string _Title = string.Empty;
        protected string _ImageUrl = string.Empty;
        protected int _TabID;
        protected string _Url = string.Empty;
        protected int _Order;
        protected string _Description = string.Empty;
        protected string _Target = string.Empty;
        protected string _CreatedBy = string.Empty;
        protected DateTime _CreationDate;

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Properties.

        public int ID
        {
            set { this._ID = value; }
            get { return this._ID; }
        }

        public int ModuleID
        {
            set { this._ModuleID = value; }
            get { return this._ModuleID; }
        }

        public string Title
        {
            set { this._Title = value; }
            get { return this._Title; }
        }

        public string ImageUrl
        {
            set { this._ImageUrl = value; }
            get { return this._ImageUrl; }
        }

        public int TabID
        {
            set { this._TabID = value; }
            get { return this._TabID; }
        }

        public string Url
        {
            set { this._Url = value; }
            get { return this._Url; }
        }

        public int Order
        {
            set { this._Order = value; }
            get { return this._Order; }
        }

        public string Description
        {
            set { this._Description = value; }
            get { return this._Description; }
        }

        public string Target
        {
            set { this._Target = value; }
            get { return this._Target; }
        }

        public string CreatedBy
        {
            set { this._CreatedBy = value; }
            get { return this._CreatedBy; }
        }

        public DateTime CreationDate
        {
            set { this._CreationDate = value; }
            get { return this._CreationDate; }
        }


        #endregion

        //---------------------------------------------------------------------------------------------

        #region Select methods.

        public static Item Load(int iD)
        {
            string spName = "[dbo].p_LINK_Item_Load";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, iD);
            Item entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
            if (reader.Read())
            {
                entity = new Item();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("ModuleID"))) entity.ModuleID = reader.GetInt32(reader.GetOrdinal("ModuleID"));
                if (!reader.IsDBNull(reader.GetOrdinal("Title"))) entity.Title = reader.GetString(reader.GetOrdinal("Title"));
                if (!reader.IsDBNull(reader.GetOrdinal("ImageUrl"))) entity.ImageUrl = reader.GetString(reader.GetOrdinal("ImageUrl"));
                if (!reader.IsDBNull(reader.GetOrdinal("TabID"))) entity.TabID = reader.GetInt32(reader.GetOrdinal("TabID"));
                if (!reader.IsDBNull(reader.GetOrdinal("Url"))) entity.Url = reader.GetString(reader.GetOrdinal("Url"));
                if (!reader.IsDBNull(reader.GetOrdinal("Order"))) entity.Order = reader.GetInt32(reader.GetOrdinal("Order"));
                if (!reader.IsDBNull(reader.GetOrdinal("Description"))) entity.Description = reader.GetString(reader.GetOrdinal("Description"));
                if (!reader.IsDBNull(reader.GetOrdinal("Target"))) entity.Target = reader.GetString(reader.GetOrdinal("Target"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedBy"))) entity.CreatedBy = reader.GetString(reader.GetOrdinal("CreatedBy"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreationDate"))) entity.CreationDate = reader.GetDateTime(reader.GetOrdinal("CreationDate"));
            }
            reader.Close();
            dbCommand.Connection.Close();

            return entity;
        }

        //---------------------------------------------------------------------------------------------

        public static DataSet SelectBy_ModuleID(int moduleID)
        {
            string spName = "[dbo].p_LINK_Item_SelectBy_ModuleID";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.Int, moduleID);

            return db.ExecuteDataSet(dbCommand);
        }
        //---------------------------------------------------------------------------------------------

        public static DataSet SelectAll(int moduleID)
        {
            string spName = "[dbo].p_LINK_Item_SelectAll";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.Int, moduleID);
            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderAll(int moduleID)
        {
            string spName = "[dbo].p_LINK_Item_SelectAll";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.Int, moduleID);
            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
        {
            string spName = "[dbo].p_LINK_Item_SelectDynamic";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
        {
            string spName = "[dbo].p_LINK_Item_SelectDynamic";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Insert methods.

        public static int Insert(int moduleID, string title, string imageUrl, int tabID, string url, int order, string description, string target, string createdBy, DateTime creationDate)
        {
            Item entity = new Item();
            entity.ModuleID = moduleID;
            entity.Title = title;
            entity.ImageUrl = imageUrl;
            entity.TabID = tabID;
            entity.Url = url;
            entity.Order = order;
            entity.Description = description;
            entity.Target = target;
            entity.CreatedBy = createdBy;
            entity.CreationDate = creationDate;
            return entity.Insert();
        }

        public int Insert()
        {
            return this.Insert(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Insert(SqlTransaction transaction)
        {
            string spName = "[dbo].p_LINK_Item_Insert";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddOutParameter(dbCommand, "@ID", SqlDbType.Int, 4);
            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.Int, this._ModuleID);
            db.AddInParameter(dbCommand, "@Title", SqlDbType.NVarChar, this._Title);
            db.AddInParameter(dbCommand, "@ImageUrl", SqlDbType.NVarChar, this._ImageUrl);
            db.AddInParameter(dbCommand, "@TabID", SqlDbType.Int, this._TabID);
            db.AddInParameter(dbCommand, "@Url", SqlDbType.NVarChar, this._Url);
            db.AddInParameter(dbCommand, "@Order", SqlDbType.Int, this._Order);
            db.AddInParameter(dbCommand, "@Description", SqlDbType.NVarChar, this._Description);
            db.AddInParameter(dbCommand, "@Target", SqlDbType.VarChar, this._Target);
            db.AddInParameter(dbCommand, "@CreatedBy", SqlDbType.NVarChar, this._CreatedBy);
            db.AddInParameter(dbCommand, "@CreationDate", SqlDbType.DateTime, this._CreationDate);

            if (transaction != null)
            {
                db.ExecuteNonQuery(dbCommand, transaction);
                this._ID = (int)db.GetParameterValue(dbCommand, "@ID");
                return this._ID;
            }
            else
            {
                db.ExecuteNonQuery(dbCommand);
                this._ID = (int)db.GetParameterValue(dbCommand, "@ID");
                return this._ID;
            }
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Update methods.

        public static int Update(int iD, int moduleID, string title, string imageUrl, int tabID, string url, int order, string description, string target, string createdBy, DateTime creationDate)
        {
            Item entity = new Item();
            entity.ID = iD;
            entity.ModuleID = moduleID;
            entity.Title = title;
            entity.ImageUrl = imageUrl;
            entity.TabID = tabID;
            entity.Url = url;
            entity.Order = order;
            entity.Description = description;
            entity.Target = target;
            entity.CreatedBy = createdBy;
            entity.CreationDate = creationDate;
            return entity.Update();
        }

        public int Update()
        {
            return this.Update(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Update(SqlTransaction transaction)
        {
            string spName = "[dbo].p_LINK_Item_Update";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, this._ID);
            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.Int, this._ModuleID);
            db.AddInParameter(dbCommand, "@Title", SqlDbType.NVarChar, this._Title);
            db.AddInParameter(dbCommand, "@ImageUrl", SqlDbType.NVarChar, this._ImageUrl);
            db.AddInParameter(dbCommand, "@TabID", SqlDbType.Int, this._TabID);
            db.AddInParameter(dbCommand, "@Url", SqlDbType.NVarChar, this._Url);
            db.AddInParameter(dbCommand, "@Order", SqlDbType.Int, this._Order);
            db.AddInParameter(dbCommand, "@Description", SqlDbType.NVarChar, this._Description);
            db.AddInParameter(dbCommand, "@Target", SqlDbType.VarChar, this._Target);
            db.AddInParameter(dbCommand, "@CreatedBy", SqlDbType.NVarChar, this._CreatedBy);
            db.AddInParameter(dbCommand, "@CreationDate", SqlDbType.DateTime, this._CreationDate);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Delete methods.

        public static int Delete(int iD)
        {
            Item entity = new Item();
            entity.ID = iD;
            return entity.Delete();
        }

        public int Delete()
        {
            return this.Delete(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Delete(SqlTransaction transaction)
        {
            string spName = "[dbo].p_LINK_Item_Delete";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, this._ID);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        #endregion
    }
}